/*
 * Copyright (C) 2017 yixun All Rights Reserved.
 * 
 * SmsHistoryServiceImpl.java
 */
package com.yixun.qfbao.service.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.mapper.SmsHistoryMapper;
import com.yixun.qfbao.model.SmsHistory;
import com.yixun.qfbao.model.dto.SmsHistoryDto;
import com.yixun.qfbao.service.SmsHistoryService;
import com.yixun.qfbao.util.DateStyle;
import com.yixun.qfbao.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * [文件名称]<br>
 * SmsHistoryServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 消息历史 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2017-05-04 10:48:27 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("smsHistoryService")
public class SmsHistoryServiceImpl implements SmsHistoryService {
	
	@Autowired
	private SmsHistoryMapper mapper;
	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<SmsHistory> getPageModel(SmsHistoryDto entity,int begin,int end){
		Integer total = mapper.count(entity);
		List<SmsHistory> rows = mapper.list(entity,begin,end);
	    Page<SmsHistory> msr = new Page<SmsHistory>(total, rows);
		return msr;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<SmsHistory> selectListByDto(SmsHistoryDto entity){
		return mapper.selectListByDto(entity);
	}
	
	/**
	 * 查询单个
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public SmsHistory selectByDto(SmsHistoryDto entity){
		return mapper.selectByDto(entity);
	}
	
	/**
	 * @param id 搜索条件
	 * @return 分页记录
	 */
	public SmsHistory selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}
	/**
	 * @param entity 新增记录的详细信息
	 * @return 新增行数
	 */
	public SmsHistoryDto create(SmsHistoryDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity 更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(SmsHistoryDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}
    
	/**
	 * @param entity 待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(SmsHistoryDto entity) {
		return mapper.del(entity);
	}
	
	/**
	 * @param entity 新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(SmsHistoryDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	/**
	 * @param entity 根据pojo搜索如果主键为空则返回它本身
	 * @return 一行记录或本身
	 */
	@Override
	public SmsHistory selectBy(SmsHistory entity) {
		if(entity!=null&&entity.getId()!=null){
			return mapper.selectById(entity.getId());
		}
		return entity;
	}
	
	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<SmsHistoryDto> getSmsSummary(SmsHistoryDto entity){
		Date createTimeBefore = entity.getCreateTimeBefore();
		Date createTimeAfter = entity.getCreateTimeAfter();
		if(null != createTimeBefore){
			String createBefore = DateUtils.DateToString(createTimeBefore, DateStyle.YYYY_MM_DD) + " 00:00:00";
			createTimeBefore = DateUtils.StringToDate(createBefore, DateStyle.YYYY_MM_DD_HH_MM_SS);
			entity.setCreateTimeBefore(createTimeBefore);
		}
		if(null != createTimeAfter){
			String createAfter = DateUtils.DateToString(createTimeBefore, DateStyle.YYYY_MM_DD) + " 23:59:59";
			createTimeAfter = DateUtils.StringToDate(createAfter, DateStyle.YYYY_MM_DD_HH_MM_SS);
			entity.setCreateTimeAfter(createTimeAfter);
		}
		return mapper.getSmsSummary(entity);
	}

	/**
	 * 获取发送失败的短信记录
	 * 默认只查询当天
	 * @param entity
	 * @return
	 */
	@Override
	public Page<SmsHistoryDto> getSmsFailurePage(SmsHistoryDto entity, int begin, int end) {
		Date createTimeBefore = entity.getCreateTimeBefore();
		Date createTimeAfter = entity.getCreateTimeAfter();

		Calendar calendar = Calendar.getInstance();

		if(null != createTimeBefore){
			String createBefore = DateUtils.DateToString(createTimeBefore, DateStyle.YYYY_MM_DD) + " 00:00:00";
			createTimeBefore = DateUtils.StringToDate(createBefore, DateStyle.YYYY_MM_DD_HH_MM_SS);
			entity.setCreateTimeBefore(createTimeBefore);
		}else{
			String createBefore = DateUtils.DateToString(calendar.getTime(), DateStyle.YYYY_MM_DD) + " 00:00:00";
			createTimeBefore = DateUtils.StringToDate(createBefore, DateStyle.YYYY_MM_DD_HH_MM_SS);
			entity.setCreateTimeBefore(createTimeBefore);
		}
		if(null != createTimeAfter){
			String createAfter = DateUtils.DateToString(createTimeBefore, DateStyle.YYYY_MM_DD) + " 23:59:59";
			createTimeAfter = DateUtils.StringToDate(createAfter, DateStyle.YYYY_MM_DD_HH_MM_SS);
			entity.setCreateTimeAfter(createTimeAfter);
		}else{
			String createAfter = DateUtils.DateToString(calendar.getTime(), DateStyle.YYYY_MM_DD) + " 23:59:59";
			createTimeAfter = DateUtils.StringToDate(createAfter, DateStyle.YYYY_MM_DD_HH_MM_SS);
			entity.setCreateTimeAfter(createTimeAfter);
		}

		Integer total = mapper.getSmsFailureCount(entity);
		List<SmsHistoryDto> rows = mapper.getSmsFailureList(entity,begin,end);
		Page<SmsHistoryDto> msr = new Page<>(total, rows);
		return msr;
	}

    @Override
    public int selectCountByDto(SmsHistoryDto smsHistoryDto) {
        return mapper.selectCountByDto(smsHistoryDto);
    }
}
